﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Data;

namespace Accounting.Library
{
    public class BSP_BanDichVuService
    {
        /// <summary>
        /// Cap nhat tien thanh toan
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="BSP_BanDichVuId">Ma hoa don ban dich vu</param>
        /// <param name="SoTien">So tien thanh toan</param>
        public static void CapNhatTienDaThanhToan(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int BSP_BanDichVuId, double SoTien) 
        {
            Accounting.DataAccess.BSP_BanDichVu banDichVu = (from p in ctx.DataContext.BSP_BanDichVus
                                                             where (p.Khoa == BSP_BanDichVuId)
                                                              select p).Single();

            double soTienDaThanhToan = banDichVu.TongTienDaThanhToan == null ? 0 : (double) banDichVu.TongTienDaThanhToan;
            soTienDaThanhToan += SoTien;
            soTienDaThanhToan = soTienDaThanhToan > banDichVu.TongTienThanhToan ? (double)banDichVu.TongTienThanhToan : soTienDaThanhToan;

            banDichVu.TongTienDaThanhToan = soTienDaThanhToan;
            ctx.DataContext.SubmitChanges();
        }

        /// <summary>
        /// Xoa tien thanh toan
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="BSP_BanDichVuId">Ma hoa don ban dich vu</param>
        /// <param name="SoTien">So tien thanh toan</param>
        public static void XoaTienDaThanhToan(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int BSP_BanDichVuId, double SoTien)
        {
            Accounting.DataAccess.BSP_BanDichVu banDichVu = (from p in ctx.DataContext.BSP_BanDichVus
                                                             where (p.Khoa == BSP_BanDichVuId)
                                                             select p).Single();

            double soTienDaThanhToan = banDichVu.TongTienDaThanhToan == null ? 0 : (double)banDichVu.TongTienDaThanhToan;
            soTienDaThanhToan -= SoTien;
            soTienDaThanhToan = soTienDaThanhToan < 0  ? 0 : soTienDaThanhToan;

            banDichVu.TongTienDaThanhToan = soTienDaThanhToan;
            ctx.DataContext.SubmitChanges();
        }

        /// <summary>
        /// Cap nhat hoa don ban hang co kem chi phi, dung cho phan bo chi phi
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="BSP_BanDichVuId">Ma hoa don ban dich vu</param>
        /// <param name="BSP_BanHangHoaId">Ma hoa don ban hang/param>
        public static void CapNhatHoaDonBanHangKemChiPhi(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int BSP_BanDichVuId, int BSP_BanHangHoaId) 
        {
            Accounting.DataAccess.BSP_BanDichVu banDichVu = (from p in ctx.DataContext.BSP_BanDichVus
                                                             where (p.Khoa == BSP_BanDichVuId)
                                                             select p).Single();

            if (!(bool)banDichVu.LaChiPhiMuaHang)
            {
                throw new Exception("Hóa đơn này không phải là chi phí bán hàng");
            }
            else
            {
                banDichVu.HoaDonBanHangId = BSP_BanHangHoaId;
                ctx.DataContext.SubmitChanges();
            }
        }

        /// <summary>
        /// Xoa hoa don ban hang co kem chi phi
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="BSP_BanDichVuId">Ma hoa don ban dich vu</param>
        /// <param name="BSP_BanHangHoaId">Ma hoa don ban hang/param>
        public static void XoaHoaDonBanHangKemChiPhi(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int BSP_BanDichVuId, int BSP_BanHangHoaId)
        {
            Accounting.DataAccess.BSP_BanDichVu banDichVu = (from p in ctx.DataContext.BSP_BanDichVus
                                                             where (p.Khoa == BSP_BanDichVuId)
                                                             select p).Single();

            banDichVu.HoaDonBanHangId = null;
            ctx.DataContext.SubmitChanges();           
        }
    }
}
